import ItemUtil from '@/ItemUtil';
import SelectUtil from '@/SelectUtil';
import UnitSpellEvent from '@/event/UnitSpellEvent';
import { heroHelperConfig } from './config';
import BuffUtil from '@/BuffUtil';
import BaseUtil from '@/BaseUtil';
import SelectCommon from 'code/Core/Common/SelectCommon';

export default class HeroHelper {
    static DB: Map<unit, unit> = new Map();
    static 商店单位: unit = null;
    constructor() {
        se.on('bag_item_used', HeroHelper.使用物品);
        se.on('created_hero', this.createHeroHelper);
        SelectUtil.forPlayerAliveUnits((u) => {
            if (GetUnitTypeId(u) == FourCC('ngme')) {
                HeroHelper.商店单位 = u;
            }
        }, 9);
        this.助手功能();
        this.助手变形();
    }
    createHeroHelper(this: void, { player, hero }: { player: player; hero: unit }) {
        let he = CreateUnit(player, 'ehep', GetUnitX(hero), GetUnitY(hero), 0);
        HeroHelper.DB.set(he, hero);
        SuspendHeroXP(he, true);
    }
    助手功能() {
        let f = DzFrameGetPeonBar();
        DzFrameSetAbsolutePoint(f, FRAMEPOINT_BOTTOMLEFT, 0.005, 0.48);
        se.onUnitSpellEffect(HeroHelper.toBag, 'He01');
        se.onUnitSpellEffect(HeroHelper.toHero, 'He00');
        se.onUnitSpellEffect(HeroHelper.一键拾取, 'He03');
        se.onUnitSpellEffect(HeroHelper.一键出售, 'He04');
        se.onUnitSpellEffect(HeroHelper.转移指定物品, 'He05');
    }
    static 转移指定物品(this: void, d: UnitSpellEvent) {
        let item = GetSpellTargetItem();
        let hero = SelectUtil.getAnHero(GetPlayerId(GetOwningPlayer(GetTriggerUnit())));
        if (!hero) return;
        UnitAddItem(hero, item);
    }
    助手变形() {
        for (let key in heroHelperConfig) {
            let config = heroHelperConfig[key];
            if (config.buffType) {
                BuffUtil.registerBuffType(config.buffType);
            }
        }

        se.onUnitSpellEffect(HeroHelper.changeHepModel);

        BaseUtil.onTimer(2.11, () => {
            for (let [key, value] of HeroHelper.助手buff) {
                let all = SelectCommon.getAllPlayerAliveUnits();
                for (let a of all) {
                    if (value.target == 'all') {
                        BuffUtil.addBuff(a, value.buffId, key);
                    }
                }
            }
            return true;
        });
    }
    static 助手buff: Map<unit, { buffId: string; target: 'hero' | 'all' }> = new Map();
    static changeHepModel() {
        let skid = GetSpellAbilityId();
        let id = id2string(skid);
        let config = heroHelperConfig[id];
        if (config == null) return;
        let u = GetTriggerUnit();
        DzSetUnitModel(u, config.model);
        DzSetUnitPortrait(u, config.portrait);
        if (config.modelScale) {
            SetUnitScale(u, config.modelScale, config.modelScale, config.modelScale);
        }

        if (config.isAll) {
            HeroHelper.助手buff.set(u, { buffId: config.buffType.id, target: 'all' });
        } else {
            let hero = SelectUtil.getAnHero(GetPlayerId(GetOwningPlayer(u)));
            BuffUtil.addBuff(hero, config.buffType.id, u);
        }

        UnitRemoveAbility(u, 'Hqcw');
    }
    static 一键出售(this: void, d: UnitSpellEvent) {
        let u = GetTriggerUnit();
        let x = GetUnitX(u);
        let y = GetUnitY(u);
        let tarX = GetUnitX(HeroHelper.商店单位);
        let tarY = GetUnitY(HeroHelper.商店单位);
        SetUnitX(u, tarX);
        SetUnitY(u, tarY);
        for (let i = 0; i < 6; i++) {
            let item = UnitItemInSlot(u, i);
            if (item) {
                UnitDropItemTarget(u, item, HeroHelper.商店单位);
            }
        }
        SetUnitX(u, x);
        SetUnitY(u, y);
    }
    static toHero(this: void, d: UnitSpellEvent) {
        let u = d.trigUnit;
        let h = HeroHelper.DB.get(u);
        if (!h) return;
        let its = ItemUtil.getAllItemFromUnit(u);
        for (let item of its) {
            UnitAddItem(h, item);
        }
    }
    static toBag(this: void, d: UnitSpellEvent) {
        let u = d.trigUnit;
        let its = ItemUtil.getAllItemFromUnit(u);
        for (let item of its) {
            UIManager.UIBakpack.model.addItem(item, GetOwningPlayer(u), u);
        }
    }
    static 设置助手(p: player, h: unit, x: number, y: number) {
        let he = CreateUnit(p, 'ehep', x, y, 0);
        HeroHelper.DB.set(he, h);
        SuspendHeroXP(he, true);
    }
    static 使用物品(this: void, d: { playerId: number } & UIBackPackItemModel) {
        let hero = SelectUtil.getAnHero(d.playerId);
        let items: item[] = ItemUtil.getAllItemFromUnit(hero);
        if (items.length < 6) {
            let tarItem = UnitAddItemById(hero, d.id);
            UnitUseItem(hero, tarItem);
        } else {
            for (let item of items) {
                UnitRemoveItem(hero, item);
            }
            let tarItem = UnitAddItemById(hero, d.id);
            UnitUseItem(hero, tarItem);
            for (let item of items) {
                UnitAddItem(hero, item);
            }
        }
    }
    static 一键拾取(this: void, d: UnitSpellEvent) {
        let u = d.trigUnit;
        let x = GetUnitX(u);
        let y = GetUnitY(u);
        let t1 = Location(x - 600, y - 600);
        let t2 = Location(x + 600, y + 600);
        let r = RectFromLoc(t1, t2);
        ItemUtil.forItemsInRect(r, (item) => {
            UnitAddItem(u, item);
        });
        RemoveLocation(t1);
        RemoveLocation(t2);
        RemoveRect(r);
    }
}
